'use strict';
app.controller('roleCtrl',['$scope','$cookies','utilService', 'roleService','$modal', function($scope,$cookies, utilService, roleService,$modal){
    $scope.role = {};
    $scope.role.platform=$cookies.loginType;
    $scope.role.systemId=$cookies.systemId;

    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'role',
            params:$scope.role
        }).then(function(result){
        });
    }
    $scope.query();

    $scope.deleteRole = function(id, name){
        var param = {
            id:id,
            name:name
        };
        // query data.
        roleService.getCountByRoleId($scope, id).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data > 0){
                        openModal("该角色【" + name + "】正在被其他用户使用，你是否要强制清空使用该角色的用户权限并且删除角色？");
                    }else{
                        openModal("你是否要删除【" + name + "】角色？");
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }

        });

        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"删除角色",
                controller:"deleteRoleCtrl",
                message:msg,
                data:param
            });
        }
    }
}]);

app.controller('createRoleCtrl',['$scope','$cookies','$state','roleService','toaster', function($scope,$cookies,$state,roleService,toaster){
    $scope.role = {
        isSys:0,
        isCommon:0,
        platform:0
    };

    $scope.createRole = function(){
        $scope.role.systemId=$cookies.systemId;
        roleService.createRole($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.role');
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
            //$state.go('app.account');
        });
    }

}]);

app.controller('editRoleCtrl',['$scope','$state','$stateParams','roleService','toaster', function($scope,$state,$stateParams,roleService,toaster){
    $scope.role = {
        id:$stateParams.id
    };

    // query data.
    roleService.findById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.role = result.data;
                break;
            case 601:
                toaster.pop('waiting', "提示", result.message);
                $state.go('app.404');
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }

    });

    // update event.
    $scope.updateRole = function(){
        roleService.updateRole($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.role');
                    break;
                case 602:
                case 603:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

}]);

app.controller('deleteRoleCtrl', ['$scope', '$modalInstance','$state','roleService','toaster','modalData',
    function($scope, $modalInstance,$state,roleService,toaster,modalData) {
        $scope.modal = modalData;
        $scope.role = {
            id: modalData.data.id
        };
        $scope.isDelModal=true;

        //确认删除
        $scope.ok = function(){
            if(!$scope.isDelModal){
                return;
            }
            $scope.isDelModal = false;
            roleService.delete($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.isDelModal=true;
                        $scope.query();
                        break;
                    case 615:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.isDelModal=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);